#include "handwheelform.h"
#include "ui_handwheelform.h"
#include<QTimer>
#include<QMessageBox>
#include"data.h"


HandWheelForm::HandWheelForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HandWheelForm)
{
    ui->setupUi(this);


    connect(ui->pushButton_connect,&QPushButton::clicked,this,[=](){on_Button_connect_clicked();});
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[=](){on_PushButton_stop_clicked();});
    connect(ui->pushButton_cleanpos,&QPushButton::clicked,this,[=](){on_PushButton_cleanpos_clicked();});

    QTimer *timer1=new QTimer(this);
    connect(timer1,&QTimer::timeout,this,&HandWheelForm::updateWin);
    timer1->start(100);


}

HandWheelForm::~HandWheelForm()
{
    delete ui;
}

//启动
void HandWheelForm::on_Button_connect_clicked(){
    if(Data::handle!=nullptr){
        if(ui->pushButton_connect->text()=="手轮链接"){
            for(int i=0;i<3;++i){
                int enable=0;
                PMC_GetAxisEnable(Data::handle,i,&enable);
                if(enable==0){
                    QMessageBox::about(NULL,"Warn","请对AXIS0,1,2使能！！");
                    return;
                }
                PMC_SetAtype(Data::handle,i,1);
            }
            int connectAxis=-1;
            if(ui->radioButton_axis0->isChecked())  connectAxis=0;
            else if(ui->radioButton_axis1->isChecked()) connectAxis=1;
            else if(ui->radioButton_axis2->isChecked()) connectAxis=2;
            if(ui->radioButton_ab->isChecked()) PMC_SetAtype(Data::handle,3,3);
            else if(ui->radioButton_pulse->isChecked()) PMC_SetAtype(Data::handle,3,6);
            int ret= PMC_Connect(Data::handle,(float)ui->doubleSpinBox_mag->value(),3,connectAxis);
            if(ret!=0){
                warn= QMessageBox::information(this,tr("warn"),"手轮链接失败");
                return;
            }
            ui->pushButton_connect->setStyleSheet("background-color:rgb(0,255,0)");
            ui->pushButton_connect->setText("手轮断开");
            runflage=true;
        }
        else{
            int ret= PMC_Single_Cancel(Data::handle,3,2);
            if(ret!=0)  {
                warn= QMessageBox::information(this,tr("warn"),"手轮断开失败");
                return;
            }
            ui->pushButton_connect->setText("手轮链接");
            ui->pushButton_connect->setStyleSheet("background-color:rgb(255,255,255)");
            runflage=false;
        }




    }


}

//停止
void HandWheelForm::on_PushButton_stop_clicked(){
    if(Data::handle!=nullptr){
        PMC_Rapidstop(Data::handle,3);
        // ui->label_flag->setText("停止");
        // ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
    }

}

//坐标清零
void HandWheelForm::on_PushButton_cleanpos_clicked(){
    if(Data::handle!=nullptr){
        for(int i=0;i<4;++i){
            PMC_SetDpos(Data::handle,i,0);
        }
    }
}

//窗口更新
void HandWheelForm::updateWin(){

    if(Data::handle!=nullptr){
        float dpos=0;
        for(int i=0;i<4;++i){
            PMC_GetDpos(Data::handle,i,&dpos);
            QString pos = QString::number( dpos,'f',3);
            if(i==0) ui->label_xpos->setText(QString("AXIS0:%1").arg(pos));
            else if(i==1) ui->label_ypos->setText(QString("AXIS1:%1").arg(pos));
            else if(i==2) ui->label_zpos->setText(QString("AXIS2:%1").arg(pos));
            else if(i==3) ui->label_upos->setText(QString("编码器轴:%1").arg(pos));
        }
        // if(runflage){
        //     int value[4]={0,0,0,0};
        //     for(int i=0;i<4;++i){
        //         PMC_GetIfIdle(Data::handle,i,&value[i]);
        //     }

        // }

    }


}
